package team2.arsystem.GUI;

import team2.arsystem.Controller.ApplicationController;
import team2.arsystem.Utilities.CommandConstants;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class CreateNewAccount extends Activity  {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.createnewaccount);
		 final Spinner securityQnSpinner = (Spinner)findViewById(R.id.spinnerSecurityQuestion);
		 
		 	ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.security_qn_array, android.R.layout.simple_spinner_item);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			securityQnSpinner.setAdapter(adapter);
		   
	        final EditText newUserNameEditText = (EditText)findViewById(R.id.editTextNewUserName);
	        final EditText newPasswordEditText = (EditText)findViewById(R.id.editTextNewPassword);
	        final EditText confirmPasswordEditText = (EditText)findViewById(R.id.editTextConfirmPassword);
	       
	        final EditText securityQAnswerEditText = (EditText)findViewById(R.id.editTextSecurityQuestionAnswer);
	        
	        Button createAccountButton = (Button)findViewById(R.id.buttonCreateNewAccount);
	        Button cancelButton = (Button)findViewById(R.id.buttonCancelCreateAccount);
	        
	         
	        createAccountButton.setOnClickListener(new OnClickListener() {
				
	        	public void onClick(View v) {
	        		
	        	String[] newAccountCredentials = new String[5];	
	        	newAccountCredentials[0] = newUserNameEditText.getText().toString();
	        	newAccountCredentials[1] = newPasswordEditText.getText().toString();
	        	newAccountCredentials[2] = confirmPasswordEditText.getText().toString();
	        	newAccountCredentials[3] = securityQnSpinner.getSelectedItem().toString();
	           	newAccountCredentials[4] = securityQAnswerEditText.getText().toString();
	        	
	        	if((ApplicationController.getApplicationController().passCommand(CommandConstants.createNewAccount,newAccountCredentials)))
	        	{
	        		try
	        		{
	        			Dialog dialogOK = new Dialog(CreateNewAccount.this);
	        			dialogOK.setContentView(R.layout.popupbox);
	        			dialogOK.setTitle("Account Reminder System");
	        			
	        		        			
	        			TextView dialogText = (TextView)dialogOK.findViewById(R.id.textViewDialogMessage);
	        			dialogText.setText("The new account was created successfully");
	        			
	        			
	                    Button button = (Button) dialogOK.findViewById(R.id.buttonDialogOK);
	                   
	                    button.setOnClickListener(new OnClickListener() {
	                   
	                        public void onClick(View v) {
	                        	
	                        	try
	                        	{
	                        	 Class menuScreen = Class.forName("team2.arsystem.GUI."+"MenuScreen");
	                    		 Intent myIntent = new Intent(CreateNewAccount.this,menuScreen);
	                    		 startActivity(myIntent);
	                        	}
	                        	catch(Exception e){
	                    			 e.printStackTrace();
	                    		 }
	                    		 
	                        }
	                    });
	        		    
	            		 
	                    dialogOK.show();
	        		}
	        		 catch(Exception e)
	        		 {
	        			 e.printStackTrace();
	        		 }
	        	
	        	}
	        		
				}
			});
	        
	        cancelButton.setOnClickListener(new OnClickListener() {
				
	        	public void onClick(View v) {
	        		
	        		try
                 	{
                        	 Class loginScreen = Class.forName("team2.arsystem.GUI."+"LoginScreen");
                    		 Intent myIntent = new Intent(CreateNewAccount.this,loginScreen);
                    		 startActivity(myIntent);
                        	}
                        	catch(Exception e)
                        	{
                    			 e.printStackTrace();
                    		 }
                    		 
                        }
        	
	        		
	        	
	        	
				});
		
	}

	
}
